#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys, os, re, string

reload(sys)
sys.setdefaultencoding('utf8')

def music():
    '''return (music play, playing)
        mocp, audacious2, rhythmbox, xmms2, Quod Libet'''
    ps_popen    = os.popen('ps -A')
    ps_text     = ps_popen.read()
    ps_popen.close()

    if re.compile('mocp').search(ps_text) != None:
        '''mocp'''
        mocp_popen  = os.popen('mocp --info')
        mocp_text   = mocp_popen.read()
        mocp_popen.close()

        title = re.compile('SongTitle: *(.+)').search(mocp_text)

        if title == None or title.group(1) == ' ':
            try:
                return ('MOCP', re.compile('File: *.*/(.+)').search(mocp_text).group(1))
            except:
                return ('NULL', 'NULL')
        else:
            return ('MOCP', title.group(1))

    elif re.compile('quodli').search(ps_text) != None:
        '''Quod Libet'''
        quodlibet_popen    = os.popen('''dbus-send --session --print-reply --dest=net.sacredchao.QuodLibet /net/sacredchao/QuodLibet net.sacredchao.QuodLibet.CurrentSong''')
        quodlibet_text     = quodlibet_popen.read()
        quodlibet_popen.close()

        artist  = re.compile('string "artist"\n.+string "(.+)"').search(quodlibet_text)
        album   = re.compile('string "album"\n.+string "(.+)"').search(quodlibet_text)
        title   = re.compile('string "title"\n.+string "(.+)"').search(quodlibet_text)

        artist_text = ''
        album_text = ''
        title_text = ''

        if artist != None:
            artist_text = artist.group(1)
        if album != None:
            album_text = album.group(1)
        if title != None:
            title_text = title.group(1)

        try:
            return ('Quod Libet', artist_text+'-'+album_text+'-'+title_text)
        except:
            return ('NULL', 'NULL')

    elif re.compile('audacious2').search(ps_text) != None:
        '''audacious2'''
        audacious2_popen    = os.popen('audtool2 --current-song')
        audacious2_text     = audacious2_popen.read()
        audacious2_popen.close()

        try:
            return ('Audacious2', audacious2_text.strip())
        except:
            return ('NULL', 'NULL')

    elif re.compile('rhythmbox').search(ps_text) != None:
        '''rhythmbox'''
        rhythmbox_popen    = os.popen('rhythmbox-client --no-start --print-playing-format %aa-%tt')
        rhythmbox_text     = rhythmbox_popen.read()
        rhythmbox_popen.close()

        try:
            return ('Rhythmbox', rhythmbox_text.strip())
        except:
            return ('NULL', 'NULL')

    elif re.compile('xmms2d').search(ps_text) != None:
        '''xmms2'''
        xmms2_popen    = os.popen('xmms2 info')
        xmms2_text     = xmms2_popen.read()
        xmms2_popen.close()

        artist  = re.compile('\[plugin/id3v2\] artist = (.+)').search(xmms2_text)
        album   = re.compile('\[plugin/id3v2\] album = (.+)').search(xmms2_text)
        title   = re.compile('\[plugin/id3v2\] title = (.+)').search(xmms2_text)

        if album == None or artist == None or title ==None:
            try:
                return ('XMMS2', re.compile('\[server\] url = *.*/(.+)').search(xmms2_text).group(1))
            except:
                return ('NULL', 'NULL')
        else:
            try:
                return ('XMMS2', artist.group(1)+'-'+album.group(1)+'-'+title.group(1) )
            except:
                return ('NULL', 'NULL')

    else:
        return ('NULL', 'NULL')
